//分页参数设置 这些全局变量关系到分页的功能
var startAllAppoint = 1;
var limitAllAppoint = 10;
var currentPageAllAppoint = 1;
var totalPageAllAppoint = 0;
var dataLength = 0;
layui.config({
	base : "js/"
}).use(['form','layer','jquery','laypage'],function(){
	var form = layui.form(),
		layer = parent.layer === undefined ? layui.layer : parent.layer,
		laypage = layui.laypage,
		$ = layui.jquery;

	//加载页面数据
	getPageList();
	toPage();
	

	//查询
	$(".search_btn").click(function(){
		startAllAppoint =1;
		currentPageAllAppoint =1;
		getPageList();
		
		toPage();
	})

	//添加会员
	$(".usersAdd_btn").click(function(){
		var index = layui.layer.open({
			title : "添加公寓",
			type : 2,
			content : "apartmentAdd.html",
			success : function(layero, index){
				layui.layer.tips('点击此处返回文章列表', '.layui-layer-setwin .layui-layer-close', {
					tips: 3
				});
			}
		})
		//改变窗口大小时，重置弹窗的高度，防止超出可视区域（如F12调出debug的操作）
		$(window).resize(function(){
			layui.layer.full(index);
		})
		layui.layer.full(index);
	})
	//设置
	$("body").on("click",".users_detail",function(){ 
		var _this = $(this);
		var index = layui.layer.open({
			title : "公寓楼层设置",
			type : 2,
			content : "apartmentfloorList.html?objId="+_this.attr("data-id"),
			success : function(layero, index){
				layui.layer.tips('点击此处返回公寓列表', '.layui-layer-setwin .layui-layer-close', {
					tips: 3
				});
			}
		})
		//改变窗口大小时，重置弹窗的高度，防止超出可视区域（如F12调出debug的操作）
		$(window).resize(function(){
			layui.layer.full(index);
		})
		layui.layer.full(index);
	})

    //全选
	form.on('checkbox(allChoose)', function(data){
		var child = $(data.elem).parents('table').find('tbody input[type="checkbox"]:not([name="show"])');
		child.each(function(index, item){
			item.checked = data.elem.checked;
		});
		form.render('checkbox');
	});

	//通过判断文章是否全部选中来确定全选按钮是否选中
	form.on("checkbox(choose)",function(data){
		var child = $(data.elem).parents('table').find('tbody input[type="checkbox"]:not([name="show"])');
		var childChecked = $(data.elem).parents('table').find('tbody input[type="checkbox"]:not([name="show"]):checked')
		if(childChecked.length == child.length){
			$(data.elem).parents('table').find('thead input#allChoose').get(0).checked = true;
		}else{
			$(data.elem).parents('table').find('thead input#allChoose').get(0).checked = false;
		}
		form.render('checkbox');
	})

	//操作
	$("body").on("click",".users_edit",function(){  //编辑
		var _this = $(this);
		var index = layui.layer.open({
		 	id: 'insert-form',//定义子页面的ID标记
			title : "编辑公寓层数",
			type : 2,
			content : "apartmentEdit.html?objId="+_this.attr("data-id"),
			success : function(layero, index){
				$.ajax({
			        url: API_URL_HEAD+"apartmentInfo/detail/"+_this.attr("data-id"),
			        type: 'get',
			        async: false,
			        contentType: 'application/json; charset=utf-8',
			        dataType: 'json',
			        success: function (data) {
			        	var vo = data.data;
			        	$(".apartmentSn", layero.find("iframe")[0].contentWindow.document).val(data.data.apartmentSn);
			        	$(".apartmentName", layero.find("iframe")[0].contentWindow.document).val(data.data.apartmentName);
			        	$(".apartmentAdder", layero.find("iframe")[0].contentWindow.document).val(data.data.apartmentAdder);
			 			$(".apartmentFloors", layero.find("iframe")[0].contentWindow.document).val(data.data.apartmentFloors);
						$(".remark", layero.find("iframe")[0].contentWindow.document).val(data.data.remark);
						$(".apartmentArea", layero.find("iframe")[0].contentWindow.document).val(data.data.apartmentArea);
						layui.layer.tips('点击此处返回列表', '.layui-layer-setwin .layui-layer-close', {
							tips: 3
						});
			        }
			   });
				layui.layer.tips('点击此处返回公寓列表', '.layui-layer-setwin .layui-layer-close', {
					tips: 3
				});
			}
		})
		//改变窗口大小时，重置弹窗的高度，防止超出可视区域（如F12调出debug的操作）
		$(window).resize(function(){
			layui.layer.full(index);
		})
		layui.layer.full(index);
	})

	$("body").on("click",".users_del",function(){  //删除
		var _this = $(this);
		layer.confirm('确定删除此用户？',{icon:3, title:'提示信息'},function(index){
				$.ajax({
			        url: API_URL_HEAD+"apartmentInfo/deleteById/"+_this.attr("data-id"),
			        type: 'get',
			        async: false,
			        contentType: 'application/json; charset=utf-8',
			        dataType: 'json',
			        success: function (data) {
			        	var vo = data.data;
						layer.close(index);
						startAllAppoint =1;
						currentPageAllAppoint =1;
						getPageList();
						toPage();
			        }
			  });

		});
	})

     function getPageList(){
		var data={
			"searchKey":$(".search_input").val()
		};
		var pageData ={
			"page":startAllAppoint,
			"limit":limitAllAppoint
			};
			$.extend(data, pageData);
		    $.ajax({
	        url: API_URL_HEAD+"apartmentInfo/queryPage",
	        type: 'post',
	        async: false,
	        data :JSON.stringify(data),
	        contentType: 'application/json; charset=utf-8',
	        dataType: 'json',
	        success: function (data) {
	        	var list = data.data.list;
	        	renderData(list);
	        	startAllAppoint = data.data.pageNum;//当前页数(后台返回)
	        	totalPageAllAppoint = data.data.pages;//总页数(后台返回)
	        }
	    });
	}

function renderData(data){
	var dataHtml = '';
	if(data.length != 0){
		for(var i=0;i<data.length;i++){
			dataHtml += '<tr>'
			    	+  '<td><input type="checkbox" name="checked" lay-skin="primary" lay-filter="choose"></td>'
			    	+  '<td>'+data[i].apartmentSn+'</td>'
			    	+  '<td>'+data[i].apartmentName+'</td>'
			    	+  '<td>'+data[i].apartmentAdder+'</td>'
			    	+  '<td>'+data[i].apartmentFloors+'</td>'
			    	+  '<td>'+data[i].remark+'</td>'
			    	+  '<td>'
					+    '<a class="layui-btn layui-btn-mini users_edit" data-id="'+data[i].id+'"><i class="iconfont icon-edit"></i> 编辑</a>'
					+    '<a class="layui-btn layui-btn-mini users_detail" data-id="'+data[i].id+'"><i class="layui-icon">&#xe614;</i>楼层设置</a>'
					+    '<a class="layui-btn layui-btn-danger layui-btn-mini users_del" data-id="'+data[i].id+'"><i class="layui-icon">&#xe640;</i> 删除</a>'
			        +  '</td>'
			    	+'</tr>';
		}
	}else{
		dataHtml = '<tr><td colspan="8">暂无数据</td></tr>';
	}
	$(".vo_content").html(dataHtml);
}
function toPage(){
    var nums = 10;
    //调用分页
      laypage({
        cont: 'page'
        ,pages: totalPageAllAppoint //得到总页数
        ,curr: currentPageAllAppoint
        ,skip: true
        ,jump: function(obj, first){
            currentPageAllAppoint = obj.curr;
            startAllAppoint = obj.curr;
          if(!first){ //一定要加此判断，否则初始时会无限刷新
          	getPageList();//一定要把翻页的ajax请求放到这里，不然会请求两次。
               }
          	form.render();
            }
         });
};   
})